﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Utils;
using DevExpress.XtraRichEdit.API.Native;

namespace GoobizFrame.Windows.Controls.CKEditor
{
    public partial class XRichEditControl : DevExpress.XtraEditors.XtraUserControl
    {

        private CharacterProperties _CharProperties;
        private bool updatingFontName = false;
        private bool updatingFontSize = false;


        public XRichEditControl()
        {
            InitializeComponent();

            SetupFontComboBox();

        }

        #region Properties
        /// <summary>
        /// Get/Set the current font name.
        /// </summary>
        [Browsable(false)]
        public FontFamily FontName
        {
            get
            {
                try
                {
                    if (! richEditControl.ContainsFocus)
                        return null;                 

                    string name = _CharProperties.FontName;
                    if (name == "") return null;
                    return new FontFamily(name);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }

            set
            {
                if (value != null)
                    _CharProperties.FontName = ((FontFamily)value).GetName(0);
            }
        }

       

        #endregion

        /// <summary>
        /// Populate the font combo box and autocomplete handlers.
        /// Add a text changed handler to the font combo box to handle new font selections.
        /// </summary>
        private void SetupFontComboBox()
        {
            //AutoCompleteStringCollection ac = new AutoCompleteStringCollection();
            foreach (FontFamily fam in FontFamily.Families)
            {
                biComboBox_Font.Items.Add(fam.Name);
                //ac.Add(fam.Name);
            }

            //DevExpress Items
            biComboBox_Font.Leave += new EventHandler(biComboBox_Font_Leave);
        }

        /// <summary>
        /// Update the font combo box.
        /// Sets a flag to indicate that the combo box is updating, and should 
        /// not update the editor's selection.
        /// </summary>
        private void UpdateFontComboBox()
        {

            FontFamily fam = FontName;
            if (fam != null)
            {
                string fontname = fam.Name;
                if (fontname != "" + biComboBox_Font.Editable)
                    biFont.EditValue = fontname;
            }

        }

        /// <summary>
        /// Called when the font combo box has changed.
        /// Ignores the event when the timer is updating the font combo Box 
        /// to synchronize the editor selection with the font combo box.
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">EventArgs</param>
        void biComboBox_Font_Leave(object sender, EventArgs e)
        {
            FontFamily _FontFamily = new FontFamily( System.Drawing.Text.GenericFontFamilies.SansSerif);
            try
            {
                _FontFamily = new FontFamily("" + biFont.EditValue);
            }
            catch (Exception)
            {
                if (FontName!=null)
                    biFont.EditValue = FontName.GetName(0);
                return;
            }
            FontName = _FontFamily;
        }
      
        private void barManager1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                #region Options
                case "biRuler":
                    if (richEditControl.Options.HorizontalRuler.Visibility == DevExpress.XtraRichEdit.RichEditRulerVisibility.Visible)
                    {
                        richEditControl.Options.HorizontalRuler.Visibility = DevExpress.XtraRichEdit.RichEditRulerVisibility.Hidden;
                        richEditControl.Options.VerticalRuler.Visibility = DevExpress.XtraRichEdit.RichEditRulerVisibility.Hidden;
                    }
                    else
                    {
                        richEditControl.Options.HorizontalRuler.Visibility = DevExpress.XtraRichEdit.RichEditRulerVisibility.Visible;
                        richEditControl.Options.VerticalRuler.Visibility = DevExpress.XtraRichEdit.RichEditRulerVisibility.Visible;
                    }
                    break;
                #endregion

                #region edit bar
                case "biCut":
                    break;
                case "biCopy":
                    break;
                case "biPaste":
                    break;
                case "biHyperLink":
                    break;
                case "biImage":
                    break;
                case "biUndo":
                    break;
                case "biRedo":
                    break;
                case "biSearch":
                    break;
                #endregion

                #region Format
                case "biFont":                    
                    break;
                case "biFontSize":                    
                    break;
                case "biBold":                    
                    break;
                case "biItalic":                    
                    break;
                case "biUnderline":                    
                    break;
                case "biAlignLeft":                    
                    break;
                case "biAlignCenter":                    
                    break;
                case "biAlignRight":                    
                    break;
                case "biAlignJustify":                    
                    break;
                case "biColor":                    
                    break;
                case "biBackColor":                    
                    break;
                case "biList":                    
                    break;
                case "biOrderedList":                    
                    break;
                case "biOutdent":                    
                    break;
                case "biIndent":                    
                    break;

                #endregion
            }
        }

    

       

        //private void richEditControl_SelectionChanged(object sender, EventArgs e)
        //{
        //    Document doc = richEditControl.Document;
        //    DocumentRange range = richEditControl.Document.Selection;

        //    _CharProperties = doc.BeginUpdateCharacters(range);
        //    UpdateFontComboBox();
       
                   
        //    biRuler.Checked =
        //        (richEditControl.Options.HorizontalRuler.Visibility == DevExpress.XtraRichEdit.RichEditRulerVisibility.Visible)
        //        &&
        //        (richEditControl.Options.VerticalRuler.Visibility == DevExpress.XtraRichEdit.RichEditRulerVisibility.Visible);
        //}
    }


   
}
